package com.patMall.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.patMall.entity.Concern;
import com.patMall.service.ConcernService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 750909637@qq.com
 * @PackageName patMall
 * @Package com.patMall.controller
 * @Date 2024/2/17 17:43
 * @Version 1.0
 */
@Controller
@RequestMapping("concern")
public class ConcernController {
    @Resource
    private ConcernService concernService;
    @PostMapping("getConcernUser")
    @ResponseBody
    public Object getConcernUser(Integer userId){
        List<Concern> concernList = concernService.getConcernUser(userId);
        Map map = new HashMap();
        if (concernList != null) {
            map.put("data", concernList);
            map.put("msg", "success");
        } else{
            map.put("msg","fail");
        }
        return map;
    }
    @PostMapping("addConcern")
    @ResponseBody
    public String addConcern(Concern concern){
        int count = concernService.addConcern(concern);
        if (count == 1){
            return "success";
        }else {
            return "fail";
        }
    }

    @PostMapping("updateConcern")
    @ResponseBody
    public String updateConcern(Concern concern){
        int count = concernService.updateConcern(concern);
        if (count == 1){
            return "success";
        }else {
            return "fail";
        }
    }

    @PostMapping("/findAllConcern")
    @ResponseBody
    public Object findAllConcern(int page,int limit,String role) {
        Map map = new HashMap();
        PageHelper.startPage(page,limit);
        List<Concern> concernList = concernService.findConcern(role);
        Page<Concern> pageList =(Page<Concern>) concernList;
        long count = pageList.toPageInfo().getTotal();
        map.put("code",0);
        map.put("msg", "这是数据传来的！");
        map.put("count", count);
        map.put("data", pageList);
        return map;
    }
    @PostMapping("/findConcernUser")
    @ResponseBody
    public Object findConcernUser(int page,int limit,String username) {
        Map map = new HashMap();
        PageHelper.startPage(page,limit);
        List<Concern> concernList = concernService.findConcernUser(username);
        System.err.println(concernList);
        Page<Concern> pageList =(Page<Concern>) concernList;
        System.err.println(pageList);
        long count = pageList.toPageInfo().getTotal();
        map.put("code",0);
        map.put("msg", "这是数据传来的！");
        map.put("count", count);
        map.put("data", pageList);
        return map;
    }

}
